# Contributor: Will Sinatra <wpsinatra@gmail.com>
# Maintainer: Will Sinatra <wpsinatra@gmail.com>
pkgname=grocy
pkgver=4.0.3
pkgrel=2
pkgdesc="web-based self-hosted groceries & household management solution for your home"
url="https://grocy.info/"
license="MIT"
arch="noarch"
_php=php82
makedepends="composer yarn $_php-dev $_php-sqlite3 $_php-gd $_php-intl"
depends="$_php
	$_php-fpm
	$_php-ctype
	$_php-fileinfo
	$_php-gd
	$_php-iconv
	$_php-intl
	$_php-mbstring
	$_php-pdo_sqlite
	$_php-simplexml
	$_php-sqlite3
	$_php-tokenizer
	sqlite
	"
source="$pkgname-$pkgver.zip::https://github.com/grocy/grocy/releases/download/v$pkgver/grocy_$pkgver.zip
	$pkgname-$pkgver.tar.gz::https://github.com/grocy/grocy/archive/refs/tags/v$pkgver.tar.gz
	grocy_nginx.conf
	grocy_nginx_fpm.conf"
options="!check" #no checks defined
subpackages="$pkgname-nginx:_nginx"

unpack() {
	#Web application is pre-packaged
	#Default unpack dumps this directly into the srcdir
	unzip $srcdir/$pkgname-$pkgver.zip -d $srcdir/$pkgname-$pkgver

	#But the web application lacks yarn & composer files to build with
	#so we pull and extract both
	mkdir $srcdir/source
	tar -xzvf $srcdir/$pkgname-$pkgver.tar.gz -C $srcdir/source
}

prepare() {
	default_prepare

	#Pull yarn & composer files from tarball, and insert into webapp directory
	for f in yarn.lock composer.lock composer.json composer.lock package.json; do
		cp $srcdir/source/$pkgname-$pkgver/$f $srcdir/$pkgname-$pkgver/
	done
}

build() {
	$_php /usr/bin/composer.phar dump-autoload --no-cache --no-interaction --no-dev --optimize

	yarn install --modules-folder public/node_modules --production
	yarn cache clean
}

package() {
	_instdir="$pkgdir"/usr/share/webapps/grocy
	mkdir -p "$_instdir" "$pkgdir"/etc/webapps/grocy "$pkgdir"/var/lib/webapps

	cp -r "$builddir"/* "$_instdir"
	mv "$pkgdir"/usr/share/webapps/grocy/data "$pkgdir"/var/lib/webapps/grocy

	ln -s /var/lib/webapps/grocy "$pkgdir"/usr/share/webapps/grocy/data
	ln -s /etc/webapps/grocy/config.php "$pkgdir"/var/lib/webapps/grocy/config.php

	mv "$builddir"/config-dist.php "$pkgdir"/etc/webapps/grocy/config.php

	chown -R root:www-data "$pkgdir"/usr/share/webapps/grocy
	chown -R root:www-data "$pkgdir"/var/lib/webapps/grocy
	chmod -R 0775 "$pkgdir"/var/lib/webapps/grocy
}

_nginx() {
	pkgdesc="Nginx configuration for Grocy"
	depends="nginx !grocy-lighttpd"
	install="$subpkgname".pre-install

	install -d "$subpkgdir"/etc/nginx/sites-available
	install -d "$subpkgdir"/etc/$_php/conf-available
	install -Dm0644 "$srcdir"/grocy_nginx.conf -t "$subpkgdir"/etc/nginx/sites-available
	install -Dm0644 "$srcdir"/grocy_nginx_fpm.conf -t "$subpkgdir"/etc/$_php/conf-available
}

sha512sums="
c542ceace38eac1a0534c5d08752921a3af60e85086f1f8618f780e3f757e5fc277f27ef8b5c594d2dec63cbd0ba4978473a50698b2fad69808b8d63904eb71c  grocy-4.0.3.zip
5a334252b4a6425ae31885021cb10fa7a769f39924582bcbab6c263303c8eccfe77a1b70a005c3f00a982125d991c43db75760d98cecf00b0eaab4326a516e88  grocy-4.0.3.tar.gz
20f698a7b634ef6390f31275f2c0f8ca645e626521cb252c5c248c33616bd744ec0270f62bd7ffb3b56220dc37829ec8cc2692789ea1efffad8ba098e4c5caae  grocy_nginx.conf
707cb5c9837d75f6506b390207e544456d75fc2851791060261286d4cfa9534a785b84f26769d8d706e045a0f0cd1a0f3a30dcd3468b7c77ab237492580dc151  grocy_nginx_fpm.conf
"
